package com.sansan.qiangji.controller.warehouse;

import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.warehouse.PurchaseOrderAddForm;
import com.sansan.qiangji.form.warehouse.PurchaseOrderEditForm;
import com.sansan.qiangji.form.warehouse.PurchaseOrderListForm;
import com.sansan.qiangji.realize.warehouse.PurchaseOrderReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("purchaseOrder")
@CrossOrigin()
@Slf4j
public class PurchaseOrderController {

    @Autowired
    private PurchaseOrderReal purchaseOrderReal;


    @PostMapping("/addPurchaseOrder")
    public ResultVO<Object> addPurchaseOrder(@Valid @RequestBody PurchaseOrderAddForm purchaseOrderAddForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【采购单新增】参数不正确，purchaseOrderAddForm={}",purchaseOrderAddForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR);
        }
        purchaseOrderReal.addPurchaseOrder(purchaseOrderAddForm);
        return ResultVOUtil.success(null);
    }


    @PostMapping("/getPurchaseOrderList")
    public ResultVO<Object> getPurchaseOrderList(@Valid @RequestBody PurchaseOrderListForm form, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【采购单查询】参数不正确，purchaseOrderAddForm={}",form);
            throw new ServiceException(ResultEnum.PARAM_ERROR);
        }
        return ResultVOUtil.success(purchaseOrderReal.purchaseOrderList(form));
    }


    @PostMapping("/editPurchaseOrder")
    public ResultVO<Object> editPurchaseOrder(@Valid @RequestBody PurchaseOrderEditForm purchaseOrderEditForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【采购单修改】参数不正确，purchaseOrderAddForm={}",purchaseOrderEditForm);
            throw new ServiceException(ResultEnum.PARAM_ERROR);
        }
        purchaseOrderReal.editPurchaseOrder(purchaseOrderEditForm);
        return ResultVOUtil.success(null);
    }


    @PostMapping("/deletePurchaseOrder")
    public ResultVO<Object> deletePurchaseOrder(@RequestParam(value = "purchaseOrderId") Integer purchaseOrderId){
        purchaseOrderReal.deletePurchaseOrder(purchaseOrderId);
        return ResultVOUtil.success(null);
    }
}
